﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ProjectCloth.Entity;
using ProjectCloth.FrmUI.Common;

namespace ProjectCloth.FrmUI
{
    public partial class DraftOrderListFrm : UserControl
    {
        private ClothContext db = new ClothContext();
        public DraftOrderListFrm()
        {
            InitializeComponent();
        }

        private void QueryBtn_Click(object sender, EventArgs e)
        {
            var startTime = startDatePiker.Value.Date;
            var endTime = endDatePicker.Value.AddDays(1).Date;
            var list = (from a in db.DraftOrders.AsNoTracking().Where(o =>
                        (o.CreateTime < endTime && o.CreateTime >= startTime
                        && (string.IsNullOrEmpty(CompanyNameTxt.Text) || o.CompanyName.Contains(CompanyNameTxt.Text))
                        && (string.IsNullOrEmpty(PhoneTxt.Text) || o.Phone.Contains(PhoneTxt.Text))
                        && (string.IsNullOrEmpty(LiaisonTxt.Text) || o.Liaison.Contains(LiaisonTxt.Text))
                        && (string.IsNullOrEmpty(SummaryTxt.Text) || o.Summary.Contains(SummaryTxt.Text))
                                  )).ToList()
                        select new
                        {
                            a.OrderNo,
                            Date = a.CreateTime,
                            a.CompanyName,
                            a.Phone,
                            a.Liaison,
                            a.Summary,
                            a.TotalAmount,
                            a.SaleOrderNo
                        }).ToList();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = list;
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
               e.RowBounds.Location.Y,
               dataGridView1.RowHeadersWidth - 4,
               e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                  (e.RowIndex + 1).ToString(),
                   dataGridView1.RowHeadersDefaultCellStyle.Font,
                   rectangle,
                   dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                   TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                var main = MainFrm.CreateInstrance();
                var tab = main.Controls.Find("tabControl1", true).FirstOrDefault() as TabControl;
                var tabControl = tab;
                if (tab != null)
                {
                    tabControl = (TabControl)tab as TabControl;
                }
                if (dataGridView1.Rows[e.RowIndex].Cells["OrderNo"].Value == null) return;
                var draftOrderNo = dataGridView1.Rows[e.RowIndex].Cells["OrderNo"].Value.ToString();
                if (dataGridView1.Columns[e.ColumnIndex].Name == "CreateSaleOrderBtn")
                {
                }
                else
                {
                    new AddTab(new DraftOrderFrm(draftOrderNo), "DraftOrderFrm", "草稿单", tabControl);
                }

            }
        }
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                var main = MainFrm.CreateInstrance();
                var tab = main.Controls.Find("tabControl1", true).FirstOrDefault() as TabControl;
                var tabControl = tab;
                if (tab != null)
                {
                    tabControl = (TabControl)tab as TabControl;
                }
                if (dataGridView1.Rows[e.RowIndex].Cells["OrderNo"].Value == null) return;
                var draftOrderNo = dataGridView1.Rows[e.RowIndex].Cells["OrderNo"].Value.ToString();
                if (dataGridView1.Columns[e.ColumnIndex].Name == "CreateSaleOrderBtn")
                {
                    if (Helper.GetStringFromCell(dataGridView1.Rows[e.RowIndex].Cells["SaleOrderNo"]).Length > 0)
                    {
                        MessageBox.Show("该草稿单已生成大货单！");
                        return;
                    }
                    new AddTab(new SaleOrderFrm(1, draftOrderNo), "DraftToSaleOrderFrm", "草稿单转入大货单", tabControl);
                }
                if (dataGridView1.Columns[e.ColumnIndex].Name == "DeleteBtn")
                {
                    var dia = MessageBox.Show("确定删除草稿单！", "提示", MessageBoxButtons.YesNoCancel);
                    if (dia == DialogResult.OK)
                    {
                        var draftOrder = db.DraftOrders.Where(o => o.OrderNo == draftOrderNo).FirstOrDefault();
                        if (draftOrder != null)
                        {
                            var draftOrderItems = db.DraftOrderItems.Where(o => o.DraftOrderId == draftOrder.Id).ToList();
                            db.DraftOrderItems.RemoveRange(draftOrderItems);
                            db.DraftOrders.Remove(draftOrder);
                            var reasult = db.SaveChanges();
                            if (reasult > 0)
                            {
                                MessageBox.Show("删除草稿单成功！");
                            }
                            else MessageBox.Show("删除草稿单失败！");
                        }
                    }
                }
            }
        }
    }
}
